<?php


namespace app\common\model;

use app\admin\controller\Webconfig;
use think\facade\Db;
use think\facade\Session;

use app\common\model\Common as CommonModel;

class Site extends CommonModel
{
    protected $pk = 'site_id';

    /**
     * index页面数据获取
     * */
    public function tableData($post,$field='*',$isPage = true)
    {
        if (isset($post['limit'])) {
            $limit = $post['limit'];
        } else {
            $limit = 10;
        }

        $tableWhere = $this->tableWhere($post);
        $query      = $this
            ->field($field)
            ->where($tableWhere['where'])->whereOr($tableWhere['whereOr'])->order($tableWhere['order']);

        if ($isPage) {
            $list        = $query->paginate([
                'list_rows'=> $limit,//每页数量
                'query' => request()->param(),
            ]);
            $data        = $this->tableFormat($list->getCollection());         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $page = $list->render();
            $re['count'] = $list->total();
        } else {
            $list        = $query->select();
            $data        = $this->tableFormat($list);         //返回的数据格式化，并渲染成table所需要的最终的显示数据类型
            $re['count'] = count($list);
            $page = [];
        }

        $re['code'] = 1;
        $re['msg']  = '';
        $re['data'] = $data;
        $re['page'] = $page;
        return $re;
    }


    protected function tableWhere($post)
    {
        $where = $whereOr = [];

        if(isset($post['title']) && !empty($post['title'])){
            $where[] = ['title','like','%'.$post['title'].'%'];
        }

        $result['where']   = $where;
        $result['whereOr'] = $whereOr;

        $result['field'] = "*";
        $pk = $this->pk;
        $result['order'] = ['orderid'=>'asc',$pk => 'asc'];
        return $result;
    }

    protected function tableFormat($list)
    {
        foreach($list as $k => $v){
            $list[$k]['add_time'] = date("Y-m-d",$v['add_time']);
        }
        return $list;
    }


    public function handle($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        $act = $param['act'];

        $data['name'] = $param['name'];//站点名称
        $data['key'] = $param['key'];//站点标识
        $data['webname'] = $param['webname'];//站点标题
        $data['orderid'] = $param['orderid'];

        if($act == 'add'){
            $data['add_time'] = isset($param['add_time']) ? strtotime($param['add_time']) : time();
            Db::startTrans();
            try {
                //添加站点
                $site_id = $this->insertGetId($data);
                if(!$site_id){
                    Db::rollback();
                    $result['msg'] = '站点添加失败';
                    return $result;
                }
                $site_key = $param['key'];
                $webname = $param['webname'];
                //添加设置
                $arr = $this->commonArr($site_id,$site_key,$webname);
                $res = DB::name("webconfig")->insertAll($arr);
                if(!$res){
                    Db::rollback();
                    $result['msg'] = '站点配置添加失败';
                    return $result;
                }

                Db::commit();
                adminLog($param['name']."站点添加",$site_id);
                $result['code'] = 1;
                $result['msg'] = '站点添加成功';
                return $result;
            } catch (\Exception $e) {
                Db::rollback();
                $result['msg'] = $e->getMessage();
                return $result;
            }
        }
        if($act == 'edit'){
            $data['update_time'] = time();

            $site_id = $param['site_id'];
            $site_key = $param['key'];
            $webname = $param['webname'];

            Db::startTrans();
            try {
                //添加站点
                $res = $this->where('site_id','=',$site_id)->update($data);
                if(!$res){
                    Db::rollback();
                    $result['msg'] = '站点添加失败';
                    return $result;
                }

                //删除站点设置
                $res2 = DB::name("webconfig")->where('siteid','=',$site_id)->delete();
                if(!$res2){
                    Db::rollback();
                    $result['msg'] = '站点设置删除失败';
                    return $result;
                }

                //添加设置
                $arr = $this->commonArr($site_id,$site_key,$webname);
                $res3 = DB::name("webconfig")->insertAll($arr);
                if(!$res3){
                    Db::rollback();
                    $result['msg'] = '站点配置修改失败';
                    return $result;
                }
                Db::commit();
                $result['code'] = 1;
                $result['msg'] = '站点修改成功';

                adminLog($param['name']."站点修改",$site_id);
                return $result;
            } catch (\Exception $e) {
                Db::rollback();
                $result['msg'] = $e->getMessage();
                return $result;
            }

        }
    }


    public function commonArr($site_id,$site_key,$webname){

        $arr = [
            ['siteid'=>$site_id,'varname'=>'cfg_webname_'.$site_key,'varinfo'=>'网站名称','vargroup'=>0,'vartype'=>'string','varvalue'=>$webname,'orderid'=>1],

            ['siteid'=>$site_id,'varname'=>'cfg_weburl_'.$site_key,'varinfo'=>'网站地址','vargroup'=>0,'vartype'=>'string','varvalue'=>'','orderid'=>2],

            ['siteid'=>$site_id,'varname'=>'cfg_logo_'.$site_key,'varinfo'=>'顶部logo','vargroup'=>0,'vartype'=>'image','varvalue'=>'','orderid'=>3],

            ['siteid'=>$site_id,'varname'=>'cfg_logo2_'.$site_key,'varinfo'=>'底部logo','vargroup'=>0,'vartype'=>'image','varvalue'=>'','orderid'=>3],

            ['siteid'=>$site_id,'varname'=>'cfg_author_'.$site_key,'varinfo'=>'网站作者','vargroup'=>0,'vartype'=>'string','varvalue'=>'dycms','orderid'=>4],
            ['siteid'=>$site_id,'varname'=>'cfg_generator_'.$site_key,'varinfo'=>'程序引擎','vargroup'=>0,'vartype'=>'string','varvalue'=>'DingYiCms','orderid'=>5],
            ['siteid'=>$site_id,'varname'=>'cfg_keyword_'.$site_key,'varinfo'=>'关键字设置','vargroup'=>0,'vartype'=>'string','varvalue'=>'','orderid'=>6],
            ['siteid'=>$site_id,'varname'=>'cfg_description_'.$site_key,'varinfo'=>'网站描述','vargroup'=>0,'vartype'=>'bstring','varvalue'=>'','orderid'=>7],
            ['siteid'=>$site_id,'varname'=>'cfg_copyright_'.$site_key,'varinfo'=>'版权信息','vargroup'=>0,'vartype'=>'bstring','varvalue'=>'Copyright © 2023 鼎易科技有限公司 All rights reserved','orderid'=>8],
            ['siteid'=>$site_id,'varname'=>'cfg_icp_'.$site_key,'varinfo'=>'备案编号','vargroup'=>0,'vartype'=>'string','varvalue'=>'','orderid'=>9],
            ['siteid'=>$site_id,'varname'=>'cfg_webswitch_'.$site_key,'varinfo'=>'启用站点','vargroup'=>0,'vartype'=>'bool','varvalue'=>'Y','orderid'=>10],
            ['siteid'=>$site_id,'varname'=>'cfg_switchshow_'.$site_key,'varinfo'=>'关闭说明','vargroup'=>0,'vartype'=>'bstring','varvalue'=>'对不起，网站维护，请稍后登陆。网站维护期间对您造成的不便，请谅解！','orderid'=>11],
        ];

        return $arr;
    }


    //删除
    public function del($param){
        $result = [
            'code' => 0,
            'msg'    => '',
            'data'   => [],
        ];
        if(!isset($param['site_id']) && $param['site_id'] == ''){
            $result['msg'] = '关键参数不能为空';
            return $result;
        }
        $site_id = $param['site_id'];
        $site_one = $this -> where("site_id","=",$site_id)->find();
        if(!$site_one){
            $result['msg'] = '此站点不存在';
            return $result;
        }

        Db::startTrans();
        try {
            $res = $this -> where("site_id","=",$site_id)->delete();
            if(!$res){
                Db::rollback();
                $result['msg'] = '站点删除失败';
                return $result;
            }

            $res = DB::name("webconfig")->where("siteid","=",$site_id)->delete();
            if(!$res){
                Db::rollback();
                $result['msg'] = '站点配置删除失败';
                return $result;
            }

            Session::delete('admin_siteid');
            \think\facade\Session::save();

            Db::commit();
            adminLog($site_one['name']."站点删除",$param['site_id']);
            $result['code'] = 1;
            $result['msg'] = '站点删除成功';
            return $result;
        } catch (\Exception $e) {
            Db::rollback();
            $result['msg'] = $e->getMessage();
            return $result;
        }


    }


}